<?php

class ManageController extends AppController {

    var $name = 'Manage';
    var $uses = array('User', 'Question', 'Answer', 'Event', 'Job', 'EventUser','Message', 'Word');
    var $helper = array('Linker');
    var $layout = 'manage';
	
	function beforeFilter(){
		if (isset($this->params['url']['user_id'])) {
			$this->set('user', $this->User->findById($this->params['url']['user_id']));
		}
	}

    function index() {
        $this->paginate = array(
            'order' => array('User.created' => 'DESC'),
        );
        $this->User->recursive = 1;
        $users = $this->paginate('User');
        $this->set('users', $users);
    }

    function questions() {
        $this->Question->recursive = 1;
		$condition = array();
		if (isset($this->params['url']['user_id'])) {
			$condition['Question.user_id'] = $this->params['url']['user_id'];
		}
		if (isset($this->params['url']['id'])) {
			$condition['Question.id'] = $this->params['url']['id'];
		}
        $this->set('questions', $this->paginate('Question',$condition));
    }

    function delete_questions() {
        if (isset($this->params['url']['id'])) {
            $this->Question->delete($this->params['url']['id']);
        }
        $this->redirect($this->referer());
    }

    function users() {
        $this->User->recursive = 1;
		$conditions = array();
		if (isset($this->params['url']['id'])) {
			$conditions["User.id"] = $this->params['url']['id'];
		}
        $this->set('users', $this->paginate('User',$conditions));
    }
	function user_show(){
        $this->User->recursive = 3;

		if (isset($this->params['url']['id'])) {
			$this->set('user', $this->User->findById($this->params['url']['id']));
		}
        
	}

    function block_user() {
        if (isset($this->params['url']['id'])) {
            $this->User->id = $this->params['url']['id'];
			if($this->User->saveField('is_blocked',1)){
				$this->Session->setFlash('用户账户停用成功！');
			}else{
				$this->Session->setFlash('用户账户停用失败！');
			}
        }
		$this->redirect($this->referer());
    }

    function answers() {
        $this->Answer->recursive = 1;
		$condition = array();
		if (isset($this->params['url']['user_id'])) {
			$condition['Answer.user_id'] = $this->params['url']['user_id'];
		}
		if (isset($this->params['url']['id'])) {
			$condition['Answer.id'] = $this->params['url']['id'];
		}
        $this->set('answers', $this->paginate('Answer',$condition));
    }

    function delete_answer() {
        if (isset($this->params['url']['id'])) {
            if ($this->Answer->delete($this->params['url']['id'])) {
                $this->Session->setFlash('回答删除成功！');
            } else {
                $this->Session->setFlash('删除失败！');
            }
        }
        $this->redirect($this->referer());
    }

    function words() {
        $this->Word->recursive = 1;
		$condition = array();
		if (isset($this->params['url']['user_id'])) {
			$condition['Word.user_id'] = $this->params['url']['user_id'];
		}
		if (isset($this->params['url']['id'])) {
			$condition['Word.id'] = $this->params['url']['id'];
		}
        $this->set('words', $this->paginate('Word',$condition));
    }

    function delete_word() {
        if (isset($this->params['url']['id'])) {
            if ($this->Word->delete($this->params['url']['id'])) {
                $this->Session->setFlash('删除成功！');
            } else {
                $this->Session->setFlash('删除失败！');
            };
        }
        $this->redirect($this->referer());
    }

    function messages() {
        $this->Message->recursive = 1;
		$condition = array();
		if (isset($this->params['url']['user_id'])) {
			$condition['Message.sender_id'] = $this->params['url']['user_id'];
		}
		if (isset($this->params['url']['id'])) {
			$condition['Message.id'] = $this->params['url']['id'];
		}
        $this->set('messages', $this->paginate('Message',$condition));
    }

    public function delete_message() {

    }

    function events() {
        $this->Event->recursive = 3;
		$conditions = array();
		if (isset($this->params['url']['user_id'])) {		
			$this->paginate=array(
			'joins'=>array(
				array(
					'table'=>'event_users',
					'alias'=>'EventUser',
					'conditions'=>array(
						'EventUser.event_id = Event.id '
						)
					),
				array(
					'table'=>'users',
					'alias'=>'User',
					'conditions'=>array(
						'EventUser.user_id = User.id '
						)
					)
				)
			);
			$conditions['EventUser.user_id'] = $this->params['url']['user_id'];
			$conditions['EventUser.role'] = EventUser::sponsor;
		}
        $this->set('events', $this->paginate('Event',$conditions));
    }

    function delete_event() {
        if (isset($this->params['url']['id'])) {
            if ($this->Event->delete($this->params['url']['id'])) {
                $this->Session->setFlash('删除成功！');
            } else {
                $this->Session->setFlash('删除失败！');
            };
            ;
        }
        $this->redirect($this->referer());
    }

    public function jobs() {
        $this->Job->recursive = 1;
		$condition = array();
		if (isset($this->params['url']['user_id'])) {
			$condition['Job.user_id'] = $this->params['url']['user_id'];
		}
		if (isset($this->params['url']['id'])) {
			$condition['Job.id'] = $this->params['url']['id'];
		}
        $this->set('jobs', $this->paginate('Job',$condition));
    }

    function delete_job() {
        if (isset($this->params['url']['id'])) {
            if ($this->Job->delete($this->params['url']['id'])) {
                $this->Session->setFlash('删除成功！');
            } else {
                $this->Session->setFlash('删除失败！');
            };
            ;
        }
        $this->redirect($this->referer());
    }

}

?>